package com.sphd.emanu.activity.cardmessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.addcard.AddCardActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.bank.BankListInfo;
import com.sphd.emanu.pojo.bank.BankMessageInfo;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.TimeUtils;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 银行卡 详情
 *
 * @Author Hu
 * @Time 2017/7/25 -- 11:01.
 * @Email 87683202@qq.com
 */
public class CardMessageActivity extends MVPBaseActivity<CardMessageContract.View, CardMessagePresenter> implements CardMessageContract.View {
    Intent intent;
    Bundle bundle;
    BankListInfo bankListInfo;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.bank_money)
    TextView bankMoney;
    @BindView(R.id.bankcard_name_textview)
    TextView bankcardNameTextview;
    @BindView(R.id.card_num_textview)
    TextView cardNumTextview;
    @BindView(R.id.cardview_bank_money)
    CardView cardviewBankMoney;
    @BindView(R.id.card_name)
    MaterialEditText cardName;
    @BindView(R.id.card_first_money)
    MaterialEditText cardFirstMoney;
    @BindView(R.id.card_create_time)
    MaterialEditText cardCreateTime;
    @BindView(R.id.card_type)
    MaterialEditText cardType;
    @BindView(R.id.card_note)
    MaterialEditText cardNote;
    @BindView(R.id.change_card_message)
    Button changeCardMessage;
    @BindView(R.id.dele_card_message)
    Button deleCardMessage;
    BankMessageInfo bankMessage;
    boolean isCanDo=false;
    List<Moudle> moudles;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_message);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        bankListInfo = (BankListInfo) bundle.getSerializable(Constant.BANKLISTINFO);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanLook(moudles, PermissionConfig.MENU_FINANCE_5);
        toolbar.setSubtitle(bankListInfo.getName());
        setTitleBar(toolbar, "帐户详情");
        bankMoney.setEnabled(false);
        bankcardNameTextview.setEnabled(false);
        cardNumTextview.setEnabled(false);
        cardviewBankMoney.setEnabled(false);
        cardName.setEnabled(false);
        cardFirstMoney.setEnabled(false);
        cardCreateTime.setEnabled(false);
        cardType.setEnabled(false);
        cardNote.setEnabled(false);

        if (!isCanDo){
            changeCardMessage.setVisibility(View.GONE);
            deleCardMessage.setVisibility(View.GONE);
        }
        addData();
    }

    private void addData() {
        mPresenter.accountMessage(String.valueOf(bankListInfo.getId()));
    }

    @Override
    public void setBean(BankMessageInfo bankMessage) {
        this.bankMessage = bankMessage;
        bankMoney.setEnabled(false);
        bankcardNameTextview.setEnabled(false);
        cardNumTextview.setEnabled(false);
        cardviewBankMoney.setEnabled(false);
        cardName.setEnabled(false);
        cardFirstMoney.setEnabled(false);
        cardCreateTime.setEnabled(false);
        cardType.setEnabled(false);
        cardNote.setEnabled(false);


        bankcardNameTextview.setText(bankMessage.getBankName());
        cardNumTextview.setText(bankMessage.getAccount() + "");
        cardFirstMoney.setText(bankMessage.getInitialAmount() + "");
        cardName.setText(bankMessage.getName());
        cardNote.setText(bankMessage.getMemo());
        cardType.setText(bankMessage.getOperation());
        cardCreateTime.setText(TimeUtils.getDayAndTime(Calendar.getInstance(), bankMessage.getCreateDate())[0]);
    }

    @OnClick({R.id.change_card_message, R.id.dele_card_message})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_card_message:
                intent.setClass(context, AddCardActivity.class);
                bundle.putBoolean(Constant.IS_ADD,false);
                bundle.putSerializable(Constant.BANKMESSAGEINFO, bankMessage);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.dele_card_message:
                break;

        }
    }
}
